package org.erp.store.infrastructure.mq.service;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.erp.common.constant.RabbitmqConstant;
import org.erp.store.domain.dto.BatchStatusMqMessageDto;
import org.erp.common.enums.DeviceInventoryEvent;
import org.erp.store.infrastructure.enums.MqNotificationEvent;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;


/**
 * 通用事件MQ通知服务
 * 支持多种库存事件的MQ消息发送
 *
 * @author dingding
 * @since 2025-11-19
 */
@Slf4j
@Service
public class EventNotificationMqService {
    
    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    /**
     * 发送通用事件通知消息
     * 根据事件类型自动选择是否发送以及发送内容
     */
    public void sendEventNotificationMessage(BatchStatusMqMessageDto messageDto) {
               DeviceInventoryEvent event = messageDto.getEvent();
        try {
           MqNotificationEvent notificationEvent = MqNotificationEvent.getByDeviceEvent(event);
            log.info("发送{} MQ消息，库存数量：{}，操作人：{}",
                    notificationEvent != null ? notificationEvent.getDescription() : event.getMessage(),
                    messageDto.getStockIds().size(),
                    messageDto.getOperateName());
          
            rabbitTemplate.convertAndSend(RabbitmqConstant.ERP_PRODUCT_EXCHANGE, RabbitmqConstant.ERP_PRODUCT_ROUTING_KEY,
                    JSONObject.toJSONString(messageDto)
            );
            log.info("{} MQ消息发送成功", notificationEvent != null ? notificationEvent.getDescription() : event.getMessage());
        } catch (Exception e) {
            log.error("发送{} MQ消息失败，错误：{}", event.getMessage(), e.getMessage(), e);
            throw new RuntimeException("发送" + event.getMessage() + "消息失败", e);
        }
    }

    
   
}